/*
by @denny.dong netwenchao@hotmail.com
D23H09M45 modify active tab bug
*/
String.prototype.format = function() {
    var args = arguments;
    return this.replace(/\{(\d+)\}/g,
	function(m, i) {
	    return args[i];
	});
}
function moduleManager(tabHoster, tabWper, moduleHoster) {
    this.modules = new Object();
    this.moduleCount = 0;
    this.preModule = null;
    this.curModule = null;
    this.tabContainer = tabHoster;
    this.tabWrapper = tabWper;
    this.moduleContainer = moduleHoster;
    this.selTabClass = "";
    this.norTabClass = "";
    this.objName = "";
    this.tbLeft;
    this.tbRight;

    this.Init = function() {
    }


    this.scroll2Module = function() {
        var offst = this.curModule.getTabActPos(this.tabContainer);
        var viewWidth = $(this.tabWrapper).width() * 0.5
        var sc_left = offst - viewWidth;
        sc_left = sc_left > 0 ? sc_left : 0;
        $(this.tabWrapper).animate({ scrollLeft: sc_left }, 1000); //scrollLeft(sc_left);
    }

    this.marqModule = function(is_left) {
        if (is_left) {
            var sc_left = $(this.tabWrapper).scrollLeft();
            if (sc_left != 0)
                $(this.tabWrapper).animate({ scrollLeft: sc_left - this.curModule.getWidth() }, 500)//.scrollLeft(sc_left - this.curModule.getWidth());
        }
        else {
            var sc_left = $(this.tabWrapper).scrollLeft();
            $(this.tabWrapper).animate({ scrollLeft: sc_left + this.curModule.getWidth() }, 500); //scrollLeft(sc_left + this.curModule.getWidth());
        }
        this.opTabMover();
    }

    this.opTabMover = function() {
        if ($(this.tabContainer).width() > $(this.tabWrapper).width()) {
            $(this.tbLeft).show();
            $(this.tbRight).show();
            if ($(this.tabWrapper).scrollLeft() == 0) {
                $(this.tbLeft).removeClass("seltb_left");
                $(this.tbLeft).addClass("distb_left");
            }
            else {
                $(this.tbLeft).addClass("seltb_left");
                $(this.tbLeft).removeClass("distb_left");
            }

            var tg = $(this.tabContainer).width() - $(this.tabWrapper).width() - $(this.tabWrapper).scrollLeft();

            if (tg < 10) {
                $(this.tbRight).removeClass("seltb_right");
                $(this.tbRight).addClass("distb_right");
            }
            else {
                $(this.tbRight).addClass("seltb_right");
                $(this.tbRight).removeClass("distb_right");
            }
        }
        else {
            $(this.tbLeft).hide();
            $(this.tbRight).hide();

        }
    }

    this.exists = function(groupName) {
        if (groupName != (null || undefined)) {
            if (typeof (groupName) == "string") {
                return (this.modules[groupName] != (null || undefined));
            }
            return (groupName != (null || undefined));
        }
        return false;
    }

    this.deleteModule = function(groupName) {
        if (this.curModule != (null || undefined)) {
            if (this.curModule.group == groupName)
                this.curModule = null;

            if (this.preModule != (null || undefined) && this.preModule.group == groupName)
                this.preModule = null;
        }
        delete this.modules[groupName];
    }

    this.removeModule = function(groupName) {
        if (this.exists(groupName)) {

            var cur = this.getModule(groupName);
            cur.removeTab(this.tabContainer);
            cur.removeHtml(this.moduleContainer);
            this.deleteModule(groupName);

            if (this.exists(this.curModule)) {
                this.showModule(this.curModule.group);
                return;
            }
            if (this.exists(this.preModule)) {
                this.showModule(this.preModule.group);
                return;
            }
            var tagEle = "";
            for (var s in this.modules) {
                tagEle = s;
            }
            if (s != (null || undefined) && s.length > 0)
                this.showModule(tagEle);
        }
    }

    this.getModule = function(groupName) {
        return this.modules[groupName];
    }
    this.createModule = function(obj) {
        if (!this.exists(obj.group))
            this.modules[obj.group] = obj;
        else {
            if (this.modules[obj.group].src != obj.src)
                this.modules[obj.group].src = obj.src;
        }
        this.validateModule();
    }
    this.validateModule = function() {
        for (var i in this.modules) {
            this.modules[i].renderTab(this.tabContainer);
            this.modules[i].renderHtml(this.moduleContainer);
        }
    }

    this.activeModule = function(groupName) {
        if (this.exists(groupName)) {
            var tagModule = this.modules[groupName];
            if (this.curModule != (null || undefined) && this.curModule.group != groupName) {
                this.preModule = this.curModule;
            }
            this.curModule = tagModule;
            return true;
        }
        return false;
    }

    this.showModule = function(groupName) {
        if (this.activeModule(groupName)) {
            if (this.curModule != (null || undefined)) {
                if (this.exists(groupName)) {
                    this.curModule.showTab(this.tabContainer, this.selTabClass, this.norTabClass);
                    this.curModule.showHtml(this.moduleContainer);
                }
            }
        }
        this.resize();
        this.scroll2Module();
        this.opTabMover();
    }

    this.resize = function () {
        var ttlHeight = top.getHt();
        $(this.moduleContainer).find(".itab[gp] iframe").css("height", ttlHeight);
    }
}
function module(tabGroup, tabName, tabTitle, tabSrc, showClose) {
    this.group = tabGroup + tabName;
    this.name = tabName;
    this.title = tabTitle;
    this.src = tabSrc;
    this.states = "";
    this.showClose = (showClose != (null || undefined)) ? showClose : true;
    this.tabDefaultWidth = 100;
    this.fxTabContainer = function(tabContainer) {
        var tabs = $(tabContainer).find("li[gp]");
        if (tabs.size() > 0) {
            var sgWidth = tabs.eq(0).width();
            sgWidth = sgWidth >= 80 ? sgWidth : 80;
            var ttlwidth = tabs.size() * (sgWidth);
            $(tabContainer).width(ttlwidth);
        }
        tabs = null;
    }
    this.FxNull = function(str1, dftval) { return (str1 == null || str1 == undefined) ? dftval : str1; }
    this.isIe6 = function() { return ($.browser.msie && /^6\..*/.test(this.FxNull($.browser.version, ""))); }
    this.getHtml = function () {
        if (this.isIe6()) {
            return "<div class=\"itab\" gp=\"{0}\" style=\"display:none;\"><iframe name=\"{0}\" width=\"99%\" id=\"{0}\" src=\"{1}\" border=\"0\" scrolling=\"no\" frameBorder=\"0\" style=\"height: 509px;over-flow:hidden;\" margin=\"0\"></iframe></div>".format(this.group, this.src);
            //return "<div class=\"itab\" gp=\"{0}\" style=\"display:none;\"><iframe name=\"{0}\" width=\"99%\" id=\"{0}\" src=\"{1}\" border=\"0\" scrolling=\"no\" frameBorder=\"0\" style=\"overflow-x:hidden;overflow-y:auto\" margin=\"0\"></iframe></div>".format(this.group, this.src);
        }
        //return "<div class=\"itab\" gp=\"{0}\" style=\"display:none;\"><iframe name=\"{0}\" width=\"100%\" id=\"{0}\" src=\"{1}\" border=\"0\" scrolling=\"no\" frameBorder=\"0\" style=\"height: 509px;over-flow:hidden;\" margin=\"0\"></iframe></div>".format(this.group, this.src);
        return "<div class=\"itab\" gp=\"{0}\" style=\"display:none;\"><iframe name=\"{0}\" width=\"100%\" id=\"{0}\" src=\"{1}\" border=\"0\"  frameBorder=\"0\" style=\"height: 450px;overflow-x:hidden;overflow-y:auto\" margin=\"0\"></iframe></div>".format(this.group, this.src);
    }

    this.getTab = function() {
        if (this.showClose) {
            return "<li gp=\"{0}\" title=\"{1}\" onclick=\"CurM.showModule('{0}')\"><p>{2}</p><a href=\"javascript:\" onclick=\"CurM.removeModule('{0}');\"></a></li>".format(
			this.group,
			this.title,
			this.getName(this.name));
        }
        return "<li gp=\"{0}\" title=\"{1}\" onclick=\"CurM.showModule('{0}')\"><p>{2}</p></li>".format(
			this.group,
			this.title,
		    this.getName(this.name));
    }
    this.renderTab = function(tabContainer) {
        var tag = $(tabContainer).find("li[gp='" + this.group + "']");
        if (tag.size() <= 0) {
            $("" + this.getTab() + "").appendTo(tabContainer);
            this.fxTabContainer(tabContainer);
            return;
        }
        var tab = tag.eq(0);
        if (tab.attr("title") != this.title)
            tab.attr("title", this.title);

        var tagTxt = this.getName(this.name);
        if (tab.find("p").eq(0).text() != tagTxt)
            tab.find("p").eq(0).text(tagTxt);
    }
    this.renderHtml = function(htmlContaner) {
        var tag = $(htmlContaner).find(".itab[gp='" + this.group + "'] iframe");
        if (tag.size() <= 0) {
            $(this.getHtml()).appendTo(htmlContaner);
            return;
        }
        if (tag.attr("src") != this.src)
            tag.attr("src", this.src);
    }
    this.showTab = function(tabContainer, cls, norcls) {
        this.renderTab(tabContainer);
        var tag = $(tabContainer).find("li[gp='" + this.group + "']");
        var tagO = $(tabContainer).find("li[gp!='" + this.group + "']");
        if (tag.size() > 0) {
            tag.removeClass(norcls);
            tag.addClass(cls);
            tagO.removeClass(cls);
            tagO.addClass(norcls);
        }
    }
    this.removeTab = function(tabContainer) {
        $(tabContainer).find("li[gp='" + this.group + "']").remove();
        this.fxTabContainer(tabContainer);
    }
    this.removeHtml = function(htmlContainer) {
        $(htmlContainer).find(".itab[gp=\"" + this.group + "\"]").remove();
    }
    this.showHtml = function(htmlContaner) {
        this.renderHtml(htmlContaner);
        var tag = $(htmlContaner).find(".itab[gp='" + this.group + "']");
        var tagO = $(htmlContaner).find(".itab[gp!='" + this.group + "']");
        if (tag.size() > 0) {
            if (tagO && tagO.size() > 0) {
                tagO.css("display", "none");
            }
            tag.css("display", "block");
        }
    }
    this.getName = function(strName) {
        if (/[\u4e00-\u9fa5]{6}/.test(strName))
            return strName.substring(0, 6) + (strName.length > 6 ? ".." : "");
        else {
            if (strName.length > 6)
                return strName.substring(0, 6) + "..";
            return strName;
        }
    }
    this.getTabActPos = function(tabContainer) {
        var curTab = $(tabContainer).find("li[gp='" + this.group + "']");
        var tabArr = $(tabContainer).find("li[gp!='']");
        if (curTab.size() > 0)
            return (tabArr.index(curTab) + 1) * curTab.width();
        return -1;
    }
    this.getWidth = function(tabContainer) {
        var curTab = $(tabContainer).find("li[gp='" + this.group + "']");
        return (curTab.size() > 0) ? curTab.width() : this.tabDefaultWidth;
    }
}